const app = require('express')()
const server = require('http').createServer(app)
const { Server } = require('socket.io')
const io = new Server(server)

app.get('/', (req, res) => {
  res.sendFile(__dirname + '/index.html')
})

io.on('connection', (socket) => {
  console.log('用户连接')
  // 监听客户端发送的消息
  socket.on('chat message', (msg) => {
    // 广播给所有客户端
    io.emit('chat message', msg)
  })

  // 断开连接
  socket.on('disconnect', () => {
    console.log('用户断开连接')
  })
})


server.listen('3000', () => {
  console.log('listening on *:3000')
})
